package cn.iocoder.yudao.module.esim.controller.app.esasset;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated;
import cn.iocoder.yudao.module.esim.controller.admin.esasset.vo.ClaimESimByUserReqVo;
import cn.iocoder.yudao.module.esim.controller.admin.esasset.vo.EsAssetPageReqVO;
import cn.iocoder.yudao.module.esim.controller.app.esasset.vo.*;
import cn.iocoder.yudao.module.esim.dal.dataobject.esasset.EsAssetDO;
import cn.iocoder.yudao.module.esim.enums.ErrorCodeConstants;
import cn.iocoder.yudao.module.esim.enums.esasset.EsAssetActiveStateEnum;
import cn.iocoder.yudao.module.esim.service.esasset.EsAssetService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "用户 App - eSim资产")
@RestController
@RequestMapping("/esim/es-asset")
@Validated
@Slf4j
public class AppEsAssetController {

    @Resource
    private EsAssetService esAssetService;

    @GetMapping("/get")
    @Operation(summary = "获得eSim资产")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthenticated
    public CommonResult<AppEsAssetRespVO> getEsAsset(@RequestParam("id") String id) {
        final Long userId = getLoginUserId();
        EsAssetDO esAsset = esAssetService.getEsAsset(userId, id);
        return success(BeanUtils.toBean(esAsset, AppEsAssetRespVO.class));
    }

    @GetMapping("/get-activated-esim")
    @Operation(summary = "获得激活的eSim")
    @Parameter(name = "iccids", description = "iccids")
    @PreAuthenticated
    public CommonResult<AppEsAssetRespVO> getActivatedESim(@RequestParam(value = "iccids", required = false) Set<String> iccids) {
        final Long userId = getLoginUserId();
        final EsAssetDO activatedESim = esAssetService.getActivatedESim(userId, iccids);
        return success(BeanUtils.toBean(activatedESim, AppEsAssetRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得eSim资产分页")
    @PreAuthenticated
    public CommonResult<PageResult<AppEsAssetRespVO>> getEsAssetPage(@Valid AppEsAssetPageReqVO pageReqVO) {
        final EsAssetPageReqVO vo = BeanUtils.toBean(pageReqVO, EsAssetPageReqVO.class);
        final Long userId = getLoginUserId();
        vo.setUserId(userId);
        PageResult<EsAssetDO> pageResult = esAssetService.getEsAssetPage(vo);
        return success(BeanUtils.toBean(pageResult, AppEsAssetRespVO.class));
    }

    @PostMapping("/active")
    @Operation(summary = "激活eSim")
    @PreAuthenticated
    public CommonResult<Void> activeESim(@Valid @RequestBody AppEsAssetActiveReqVO reqVO) {
        final Long userId = getLoginUserId();
        esAssetService.activeESim(userId, reqVO);
        return success(null);
    }

    @GetMapping("/get-qrcode")
    @Operation(summary = "获取二维码")
    @PreAuthenticated
    public CommonResult<String> getQrCode(@Valid AppEsAssetGetQrCodeReqVO reqVO) {
        final Long userId = getLoginUserId();
        final String base64Image = esAssetService.getQrCode(userId, reqVO);
        return success(base64Image);
    }

    @PostMapping("/set-inuse")
    @Operation(summary = "设置在用")
    @PreAuthenticated
    public CommonResult<Void> setInUse(@Valid @RequestBody AppEsAssetSetInUseReqVO reqVO) {
        final Long userId = getLoginUserId();
        esAssetService.setInUse(userId, reqVO.getId());
        return success(null);
    }

    @PostMapping("/set-name")
    @Operation(summary = "设置名称")
    @PreAuthenticated
    public CommonResult<Void> setName(@Valid @RequestBody AppEsAssetSetNameReqVO reqVO) {
        final Long userId = getLoginUserId();
        esAssetService.setName(userId, reqVO.getId(), reqVO.getName());
        return success(null);
    }

    @PostMapping("/claim")
    @Operation(summary = "领取eSim")
    @PreAuthenticated
    public CommonResult<String> claimESimByUser(@Valid @RequestBody ClaimESimByUserReqVo reqVo) {
        final Long userId = getLoginUserId();
        final String assetId = esAssetService.createESim(reqVo.getOrderNo(), userId, reqVo.getBuyerEmail());
        return success(assetId);
    }

}